package com.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class HttpServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8380);
        System.out.println("Server started on port 8380");

        while (true) {
            try (Socket client = server.accept();
                 BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                 PrintWriter out = new PrintWriter(client.getOutputStream())) {

                // 读取请求头
                String requestLine = in.readLine();
                if (requestLine == null) continue;

                // 响应内容
                String response = "HTTP/1.1 200 OK\r\n\r\n<h1>Hello from Java Server!</h1>";
                out.println(response);
            }
        }
    }
}
